package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysRetail;
import com.ruoyi.system.mapper.SysRetailMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysRetailProductMapper;
import com.ruoyi.system.domain.SysRetailProduct;
import com.ruoyi.system.service.ISysRetailProductService;

/**
 * 散户商品管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Service
public class SysRetailProductServiceImpl implements ISysRetailProductService
{
    @Autowired
    private SysRetailProductMapper sysRetailProductMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysRetailMapper sysRetailMapper;
    /**
     * 查询散户商品管理
     *
     * @param rId 散户商品管理主键
     * @return 散户商品管理
     */
    @Override
    public SysRetailProduct selectSysRetailProductByRId(Long rId, Long proId)
    {
        Map<String, Object> params = new HashMap<>();
        params.put("rId", rId);
        params.put("proId", proId);
        return sysRetailProductMapper.selectSysRetailProductByRIdAndProId(params);
    }

    /**
     * 查询散户商品管理列表
     *
     * @param sysRetailProduct 散户商品管理
     * @return 散户商品管理
     */
    @Override
    public List<SysRetailProduct> selectSysRetailProductList(SysRetailProduct sysRetailProduct)
    {
        //获取登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //获取用户信息
        SysUser user = sysUserMapper.selectUserById(loginUser.getUserId());
        //创建一个散户信息对象
        SysRetail sysRetail = new SysRetail();
        //将用户信息中的供应商id赋给散户信息对象
        sysRetail.setSupplierId(user.getSupplierId());
        //根据供应商id查询所有散户信息
        List<SysRetail> sysRetails = sysRetailMapper.selectSysRetailList(sysRetail);
        //创建一个结果集，存放散户商品信息
        List<SysRetailProduct> result = new ArrayList<>();
        for (SysRetail sr : sysRetails) {
            //根据散户id查询散户商品信息
            SysRetailProduct parameterToSelectList = new SysRetailProduct();
            parameterToSelectList.setrContact(sysRetailProduct.getrContact());
            parameterToSelectList.setrId(sr.getrId());
            List<SysRetailProduct> sysRetailProducts = sysRetailProductMapper.selectSysRetailProductList(parameterToSelectList);
            result.addAll(sysRetailProducts);
        }
        return result;
    }

    /**
     * 新增散户商品管理
     *
     * @param sysRetailProduct 散户商品管理
     * @return 结果
     */
    @Override
    public int insertSysRetailProduct(SysRetailProduct sysRetailProduct)
    {
        return sysRetailProductMapper.insertSysRetailProduct(sysRetailProduct);
    }

    /**
     * 修改散户商品管理
     *
     * @param sysRetailProduct 散户商品管理
     * @return 结果
     */
    @Override
    public int updateSysRetailProduct(SysRetailProduct sysRetailProduct)
    {
        return sysRetailProductMapper.updateSysRetailProduct(sysRetailProduct);
    }

    /**
     * 批量删除散户商品管理
     *
     * @param rIds 需要删除的散户商品管理主键
     * @return 结果
     */
    @Override
    public int deleteSysRetailProductByRIds(Long[] rIds)
    {
        return sysRetailProductMapper.deleteSysRetailProductByRIds(rIds);
    }

    /**
     * 删除散户商品管理信息
     *
     * @param rId 散户商品管理主键
     * @return 结果
     */
    @Override
    public int deleteSysRetailProductByRId(Long rId)
    {
        return sysRetailProductMapper.deleteSysRetailProductByRId(rId);
    }

}
